package com.yq.bdm.utils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 分页工具类
 * 
 * @author shangpan
 * 
 * @date 2016年11月4日 下午12:59:00
 */
public class PageUtils implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	// 页面大小，默认值：10（通常为正整数，10、30、50 ...）
	public static final int DEFAULT_PAGE_SIZE = 10;
	
	// 当前页码（即 第几页），默认值：1（从1开始，正整数，1、2、3 ...）
	public static final int DEFAULT_CURR_PAGE = 1;
	
	// 总记录数
	private int totalCount;
	// 每页记录数
	private int pageSize;
	// 总页数
	private int totalPage;
	// 当前页数
	private int currPage;
	// 列表数据（业务数据）
	private List<?> list;

	private Map<String, Object> map;
	
	public PageUtils() {
	}

	public PageUtils(List<?> list) {
		this.list = list;
	}

	/**
	 * 分页
	 * 
	 * @param list
	 *            列表数据
	 * @param totalCount
	 *            总记录数
	 * @param pageSize
	 *            每页记录数
	 * @param currPage
	 *            当前页数
	 */
	public PageUtils(List<?> list, int totalCount, int pageSize, int currPage) {
		this.list = list;
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		this.currPage = currPage;
		this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
	}

	/**
	 * 分页
	 * 
	 * @param list
	 *            列表数据
	 * @param totalCount
	 *            总记录数
	 * @param map
	 *            参数map
	 */
	public PageUtils(List<?> list, int totalCount, Map<String, Object> map) {
		Integer currPage = Integer.valueOf(map.get("currPage").toString());
		Integer pageSize = Integer.valueOf(map.get("limit").toString());
		this.list = list;
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		this.currPage = currPage;
		this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
		this.map = map;
	}
	
	public Integer getStartRow() {
		return this.getPageSize() * (this.getCurrPage() - 1);
	}
	
	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalPage = (totalCount + pageSize - 1) / pageSize;
		this.totalCount = totalCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrPage() {
		this.currPage = currPage <= 0 ? DEFAULT_CURR_PAGE : currPage;
		if (this.totalCount > 0) {
			// 分页计算总页数算法两种方案中的第2种，totalPage = (totalCount + pageSize - 1) / pageSize 总页数算法与这里是同等的
			int totalPage = ((this.totalCount - 1) / this.getPageSize()) + 1;
			this.totalPage = totalPage;
			currPage = currPage > totalPage ? totalPage : currPage;
		}
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public List<?> getList() {
		return list;
	}

	public void setList(List<?> list) {
		this.list = list;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}
	
}
